package cc.iotkit.plugins.mattress.server;

import cc.iotkit.plugin.core.IPlugin;
import cc.iotkit.plugin.core.IPluginConfig;
import cc.iotkit.plugins.mattress.conf.MattressServerConfig;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import com.gitee.starblues.bootstrap.realize.PluginCloseListener;
import com.gitee.starblues.core.PluginCloseType;
import com.gitee.starblues.core.PluginInfo;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * mattress插件
 *
 * @author lsj
 */
@Slf4j
@Service
public class MattressPlugin implements PluginCloseListener, IPlugin {

    @Autowired
    private PluginInfo pluginInfo;
    @Autowired
    private MattressServerVerticle mattressServerVerticle;

    @Autowired
    private MattressServerConfig mattressConfig;

    @Autowired
    @AutowiredType(AutowiredType.Type.MAIN_PLUGIN)
    private IPluginConfig pluginConfig;

    private Vertx vertx;
    private String deployedId;

    @PostConstruct
    public void init() {
        vertx = Vertx.vertx();
        try {
            //获取插件最新配置替换当前配置
            Map<String, Object> config = pluginConfig.getConfig(pluginInfo.getPluginId());
            BeanUtil.copyProperties(config, mattressConfig, CopyOptions.create().ignoreNullValue());
            mattressServerVerticle.setConfig(mattressConfig);

            Future<String> future = vertx.deployVerticle(mattressServerVerticle);
            future.onSuccess((s -> {
                deployedId = s;
                log.info("mattress plugin started success");
            }));
            future.onFailure((e) -> {
                log.error("mattress plugin startup failed", e);
            });

            Future<String> future2 = vertx.deployVerticle("cc.iotkit.plugins.mattress.cilent.MattressForwardClient");
            future2.onSuccess((s -> {
                log.info("MattressForwardClient plugin started success");
            }));
            future2.onFailure((e) -> {
                log.error("MattressForwardClient plugin startup failed", e);
            });

        } catch (Throwable e) {
            log.error("mattress plugin startup error", e);
        }
    }

    @Override
    public void close(GenericApplicationContext applicationContext, PluginInfo pluginInfo, PluginCloseType closeType) {
        mattressServerVerticle.stop();
        Future<Void> future = vertx.undeploy(deployedId);
        future.onSuccess(unused -> log.info("tcp plugin stopped success"));
        if (closeType == PluginCloseType.UNINSTALL) {
            log.info("mattress plugin UNINSTALL：{}", pluginInfo.getPluginId());
        } else if (closeType == PluginCloseType.STOP) {
            log.info("mattress plugin STOP：{}", pluginInfo.getPluginId());
        } else if (closeType == PluginCloseType.UPGRADE_UNINSTALL) {
            log.info("mattress plugin UPGRADE_UNINSTALL：{}", pluginInfo.getPluginId());
        }
    }

    @Override
    public Map<String, Object> getLinkInfo(String pk, String dn) {
        return null;
    }
}
